package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {

    public void findCourseList(HttpServletRequest request, HttpServletResponse response) {

        try {

            CourseService cs = new CourseServiceImpl();
            List<Course> courseList = cs.findCourseList();

            SimplePropertyPreFilter sppf = new SimplePropertyPreFilter(Course.class,
                    "id", "course_name", "price", "sort_num", "status");
            String result = JSON.toJSONString(courseList, sppf);

            response.setContentType("application/json;charset=utf-8");

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findByCourseNameAndStatus(HttpServletRequest request, HttpServletResponse response) {

        try {
            String courseName = request.getParameter("course_name");
            String status = request.getParameter("status");

            CourseService service = new CourseServiceImpl();
            List<Course> courseList = service.findByCourseNameAndStatus(courseName, status);

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id", "course_name", "price", "sort_num", "status");
            String result = JSON.toJSONString(courseList, filter);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {

        try {
            String id = request.getParameter("id");
            CourseService service = new CourseServiceImpl();
            Course course = service.findCourseById(Integer.parseInt(id));

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name",
            "brief", "teacher_name", "teacher_info", "preview_first_field", "preview_second_field", "discounts",
                    "price", "price_tag", "course_img_url", "share_title", "share_image_title", "share_description",
                    "course_description", "status");
            String result = JSON.toJSONString(course, filter);

            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updateCourseStatus(HttpServletRequest request, HttpServletResponse response) {

        try {
            String id = request.getParameter("id");

            CourseService service = new CourseServiceImpl();
            Course course = service.findCourseById(Integer.parseInt(id));

            int status = course.getStatus();
            if (0 == status) {
                course.setStatus(1);
            } else {
                course.setStatus(0);
            }

            course.setUpdate_time(DateUtils.getDateFormat());

            Map<String, Integer> map = service.updateCourseStatus(course);

            response.getWriter().write(JSON.toJSONString(map));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
